﻿using System;
using System.Xml.Serialization;
using System.IO;
using System.Xml;

namespace TFSBuddy.Events
{
    public class EventHelper
    {
        /// <summary>
        /// Deserializes a Type
        /// </summary>
        /// <typeparam name="T">Type to deserialize</typeparam>
        /// <param name="serializedType">serialized version of the type</param>
        /// <returns>DeSerialized version of the type</returns>
        public static T CreateInstance<T>(string serializedType) where T : new()
        {
            T customType = new T();

            XmlSerializer serializer = new XmlSerializer(typeof(T));
            using (StringReader stream = new StringReader(serializedType))
            {
                XmlReader xmlReader = new XmlTextReader(stream);
                try
                {
                    customType = (T)serializer.Deserialize(xmlReader);
                }
                catch
                {
                    //TODO: Add Logging etc here...
                    throw;
                }
            }

            return customType;
        }
    }
}
